package org.stvd.common.utils;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @title VerifyUtil
 * @description 校验工具类
 * @author houzx
 * @date 2021年11月21日
 */
public class VerifyUtil {

    /**
     * 校验是否是手机号码
     * @param mobileStr
     * @return
     */
    public static boolean isMobile(String mobileStr) {
        String regExp = "^(13[0-9]|14[5-9]|15[0-35-9]|16[5-7]|17[01-8]|18[0-9]|19[0135689])\\d{8}$";
        return match(regExp, mobileStr);
    }

    /**
     * 校验是否是电子邮箱
     * @param emailStr
     * @return
     */
    public static boolean isEmail(String emailStr) {
        String regExp = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        return match(regExp, emailStr);
    }

    /**
     * 校验是否是有效的账户密码
     * 校验规则：
     * 1、必须以字母开头\n
     * 2、必须包含字母、数字、特殊字符，特殊字符支持"!@#$%^&*-_"\n
     * 3、密码长度在8~30个字符范围内\n
     *
     * @param password 账户密码
     * @return
     */
    public static boolean isPass(String password) {
        String regExp = "^(?=[A-Za-z])(?=.*\\d)(?=.*[!@#$%^&*-_])[A-Za-z\\d~!@#$%^&*-_]{8,30}$";
        return match(regExp, password);
    }

    /**
     * 校验是否是有效的Json字符串
     * @param jsonStr
     * @return
     */
    public static boolean isJson(String jsonStr) {
        boolean isJsonObject = true;
        boolean isJsonArray = true;
        try {
            JSONObject.parseObject(jsonStr);
        } catch (Exception e) {
            isJsonObject = false;
        }
        try {
            JSONObject.parseArray(jsonStr);
        } catch (Exception e) {
            isJsonArray = false;
        }
        return isJsonObject || isJsonArray;
    }
    /**
     * 校验是否是统一社会信用代码
     * @param licenceStr
     * @return
     */
    public static boolean isLicence(String licenceStr) {
        if (StringsUtil.isEmpty(licenceStr) || !Pattern.matches(BASE_CODE_REGEX, licenceStr)) {
            return false;
        }
        char[] businessCodeArray = licenceStr.toCharArray();
        char check = businessCodeArray[17];
        int sum = 0;
        for (int i = 0; i < 17; i++) {
            char key = businessCodeArray[i];
            sum += (BASE_CODES.indexOf(key) * WEIGHT[i]);
        }
        int value = 31 - sum % 31;
        return check == BASE_CODE_ARRAY[value % 31];
    }
    private static final String BASE_CODE_STRING = "0123456789ABCDEFGHJKLMNPQRTUWXY";
    private static final char[] BASE_CODE_ARRAY = BASE_CODE_STRING.toCharArray();
    private static final List<Character> BASE_CODES = new ArrayList<>();
    private static final String BASE_CODE_REGEX = "[" + BASE_CODE_STRING + "]{18}";
    private static final int[] WEIGHT = {1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28};

    static {
        for (char c : BASE_CODE_ARRAY) { 
            BASE_CODES.add(c);
        }
    }
    
    /**
     * @param regex 正则表达式字符串
     * @param str 要匹配的字符串
     * @return 如果str符合regex的正则表达式格式,返回true, 否则返回false;
     */
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }
}
